package com.bmt.pos.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bmt.common.LoginUser;
import com.bmt.core.BaseService;
import com.bmt.core.ISaveable;
import com.bmt.core.PageListInfo;
import com.bmt.exception.ServiceException;
import com.bmt.pos.bean.ModuleInfo;
import com.bmt.pos.bean.UserModule;
import com.bmt.pos.dao.UserModuleDao;

@Service("userModuleService")
public class UserModuleService extends BaseService {

	@Autowired
	private UserModuleDao userModuleDao;

	public void processUserModuleList(LoginUser loginUser) throws ServiceException {
		try {
			List<ModuleInfo> miList = userModuleDao.getmoduleInfo(loginUser.getUser(), 0);

			List<UserModule> umlist = new ArrayList<UserModule>();
			if (miList != null) {
				for (ModuleInfo mi : miList) {
					UserModule um = new UserModule();
					um.setModule(mi);
					um.setSubModuleList(userModuleDao.getmoduleInfo(loginUser.getUser(), mi.getId()));
					umlist.add(um);
				}
			}
			loginUser.setUserModuleList(umlist);
			loginUser.setUserMIList(userModuleDao.getmoduleInfo(loginUser.getUser()));
		} catch (Exception e) {
			throw new ServiceException("Get menu error", e);
		}
	}

	public void userChange(LoginUser loginUser, Integer id) throws ServiceException {
		try {
			if (id != null && id >= 0 && id < loginUser.getUserList().size()) {
				loginUser.setUser(loginUser.getUserList().get(id));
				processUserModuleList(loginUser);
			}
		} catch (Exception e) {
			throw new ServiceException("Change User Error", e);
		}
	}

	@Override
	public PageListInfo<ISaveable> getPageInfo(int pageNum) throws ServiceException {
		try {
			return userModuleDao.getPageListInfo(pageNum);
		} catch (Exception e) {
			throw new ServiceException("获取失败", e);
		}
	}
}
